
package com.sm.shoplook.service.suit.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.dao.mapper.suit.SuitCommentMapper;
import com.sm.shoplook.domain.suit.SuitComment;
import com.sm.shoplook.service.suit.ApiSuitCommentService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.suit.front.resp.ApiSuitCommentRespVo;

/**
 * @author chenpy
 * @desc 套装评论
 * @date 2018/8/29 14:57
 * @see
 */
@Service
public class ApiSuitCommentServiceImpl implements ApiSuitCommentService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiSuitCommentServiceImpl.class);
	@Autowired
	private SuitCommentMapper suitCommentMapper;

	@Override
	public ApiResult<Integer> insertSuitComment(SuitComment suitComment) throws ApiException {
		suitComment.setCreateTime(new Date());
        Integer maxFollowNum = getMaxFollowNum(suitComment.getSuitId());
        if (suitComment.getReplyCustomerId() == null) {
            maxFollowNum = 0;
        }
        suitComment.setFollowNum(maxFollowNum + 1);
		int i = suitCommentMapper.insertSelective(suitComment);
		if (i > 0) {
			return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
		}
		throw new ApiException();
	}

	@Override
	public ApiResult<List<ApiSuitCommentRespVo>> selectCommentsBySuitId(Integer suitId) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suitCommentMapper.selectCommentsBySuitId(suitId));
	}

    @Override
	public ApiResult<Integer> getSuitCommentCount(Integer suitId) {
	    SuitComment suitComment = new SuitComment();
	    suitComment.setSuitId(suitId);
	    suitComment.setStatus(1);
	    return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suitCommentMapper.selectCount(suitComment));
    }

    /**
	 * 获取当前套装评论楼层号
	 * @param suitId
	 * @return
	 */
	private Integer getMaxFollowNum(Integer suitId) {
		Integer maxFollowNum = suitCommentMapper.getMaxFollowNum(suitId);
		return maxFollowNum == null ? 0 : maxFollowNum;
	}
}
